package com.kede.system.service.impl;

import com.kede.system.domain.SysVirtual;
import com.kede.utils.ValidationUtil;
import com.kede.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.kede.system.repository.SysVirtualRepository;
import com.kede.system.service.SysVirtualService;
import com.kede.system.service.dto.SysVirtualDto;
import com.kede.system.service.dto.SysVirtualQueryCriteria;
import com.kede.system.service.mapstruct.SysVirtualMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import com.kede.utils.PageUtil;
import com.kede.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @description 服务实现
* @author Pr.li
* @date 2023-04-18
**/
@Service
@RequiredArgsConstructor
public class SysVirtualServiceImpl implements SysVirtualService {

    private final SysVirtualRepository sysVirtualRepository;
    private final SysVirtualMapper sysVirtualMapper;

    @Override
    public Map<String,Object> queryAll(SysVirtualQueryCriteria criteria, Pageable pageable){
        Page<SysVirtual> page = sysVirtualRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(sysVirtualMapper::toDto));
    }

    @Override
    public List<SysVirtualDto> queryAll(SysVirtualQueryCriteria criteria){
        return sysVirtualMapper.toDto(sysVirtualRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public SysVirtualDto findById(String name) {
        SysVirtual sysVirtual = sysVirtualRepository.findById(name).orElseGet(SysVirtual::new);
        ValidationUtil.isNull(sysVirtual.getName(),"SysVirtual","name",name);
        return sysVirtualMapper.toDto(sysVirtual);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysVirtualDto create(SysVirtual resources) {
        resources.setName(IdUtil.simpleUUID()); 
        return sysVirtualMapper.toDto(sysVirtualRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SysVirtual resources) {
        SysVirtual sysVirtual = sysVirtualRepository.findById(resources.getName()).orElseGet(SysVirtual::new);
        ValidationUtil.isNull( sysVirtual.getName(),"SysVirtual","id",resources.getName());
        sysVirtual.copy(resources);
        sysVirtualRepository.save(sysVirtual);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String name : ids) {
            sysVirtualRepository.deleteById(name);
        }
    }

    @Override
    public void download(List<SysVirtualDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (SysVirtualDto sysVirtual : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("虚拟的最小值", sysVirtual.getMin());
            map.put("虚拟的最大值", sysVirtual.getMax());
            map.put("执行频率  Cron", sysVirtual.getCron());
            map.put("创建时间", sysVirtual.getCreateTime());
            map.put("更新时间", sysVirtual.getUpdateTime());
            map.put("存放虚拟值", sysVirtual.getValue());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}